/*
   Eventually use this: http://econym.org.uk/gmap/einsert.htm
   to get circles.
 */
import com.google.maps.LatLng;
import com.google.maps.LatLngBounds;
import com.google.maps.MapType;
import com.google.maps.MapZoomEvent;
import com.google.maps.controls.MapTypeControl;
import com.google.maps.controls.PositionControl;
import com.google.maps.controls.ZoomControl;
import com.google.maps.overlays.Polygon;
import com.google.maps.overlays.PolygonOptions;
import com.google.maps.styles.FillStyle;
import com.google.maps.styles.StrokeStyle;

import mx.controls.Alert;

private var overlays:Array=new Array();

public function init():void
{
}

private function readInput():void
{
    var url:String='../assets/input.dat';
    var loadit:URLLoader=new URLLoader();
    loadit.addEventListener(Event.COMPLETE, parseInput);
    loadit.load(new URLRequest(url));
}

public var varNames:Array;
public var inputData:Array=new Array();

private function parseInput(event:Event):void
{
    var t:String=event.target.data as String;
    var lines:Array=t.split('\n');
    varNames=lines[0].split(',');
    var words:Array;
    for (var i:int=1; i < lines.length; ++i)
    {
        if (lines[i] == '')
            continue;
        words=new Array();
        words=lines[i].split(',');
        if (words.length != varNames.length)
            Alert.show('Unexpected number of data points in line ' + i + '.');
        inputData.push(words);

        var dim:Number=Number(words[7]) * Number(scale.text);
        var text:String=words[0] + '\n' +
            words[4] + '\n------------\n' +
            'Primary fuel = ' + words[5] + '\n' +
            'Capacity factor = ' + words[6] + '\n' +
            'Nameplate capacity (MW) = ' + words[7] + '\n' +
            'CO2 emission rate (lb / MWh) = ' + words[8] + '\n' +
            'Total non-renewables (MWh) = ' + words[9] + '\n' +
            'Total renewables (MWh) = ' + words[10] + '\n';
        var polygon:Polygon=makeSquare(Number(words[1]), Number(words[2]),
            dim, text);
        overlays.push(polygon);
        map.addOverlay(polygon);
    }
}

private function onMapReady(event:Event):void
{
    readInput();
    map.setCenter(new LatLng(40, 264), 4.3, MapType.PHYSICAL_MAP_TYPE);
    map.addControl(new ZoomControl());
    map.addControl(new PositionControl());
    map.addControl(new MapTypeControl());
    map.addEventListener(MapZoomEvent.ZOOM_CHANGED, handleZoom);
}

private function handleZoom(event:MapZoomEvent):void
{
    var latLngBounds:LatLngBounds=map.getLatLngBounds();
    var windWidth:Number=Math.abs(latLngBounds.getEast() - latLngBounds.getWest());
    ta.text+='Zoom = ' + map.getZoom() + '\n';
}



private function showOverlays():void
{
    for (var i:Object in overlays)
    {
        map.addOverlay(overlays[i]);
    }
}

private function hideOverlays():void
{
    for (var i:Object in overlays)
    {
        map.removeOverlay(overlays[i]);
    }
}

private function removeOverlays():void
{
    hideOverlays();
    overlays=[];
}

private function makeSquare(lat:Number, lon:Number,
    dim:Number, text:String):Polygon
{
    var polygon:Polygon=new Polygon([
        new LatLng(lat - dim / 4., lon - dim / 2.),
        new LatLng(lat + dim / 4., lon - dim / 2.),
        new LatLng(lat + dim / 4., lon + dim / 2.),
        new LatLng(lat - dim / 4., lon + dim / 2.),
        new LatLng(lat - dim / 4., lon - dim / 2.)
        ],
        new PolygonOptions({
            geodesic: false
            //strokeStyle: null 
            /* new StrokeStyle({
               color: 0x0000ff,
               thickness: 0,
             alpha: 0.7}) */,
            fillStyle: new FillStyle({
                    color: 0x000000,
                    alpha: 0.5}),
            strokeStyle: new StrokeStyle({
                    thickness: 2,
                    color: 0x000000,
                    alpha: 1,
                    pixelHinting: true
                }),
            tooltip: text
        }));
    return polygon;
}
